package io.awen.modules.scheduledTask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.awen.modules.sign.entity.SignCarLonLatEntity;
import io.awen.modules.sign.service.SignCarLonLatService;
import io.awen.modules.sign.service.SignCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

@Slf4j
@Component
@Configuration
@EnableScheduling
public class SignCarTask {

    @Autowired
    private SignCarService signCarService;
    @Autowired
    private SignCarLonLatService signCarLonLatService;

    /**
     * 凌晨2点清理3个月前的车辆经纬度信息
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void clearLonLat() {
        try {
            log.info("定时任务，清理3个月前的车辆经纬度信息开始" + new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, -90);
            signCarLonLatService.remove(
                    new QueryWrapper<SignCarLonLatEntity>().lambda()
                            .le(SignCarLonLatEntity::getDataTime, calendar.getTime())
            );
            log.info("定时任务，清理3个月前的车辆经纬度信息结束" + new Date());
        } catch (Exception e) {
            throw new RuntimeException("定时任务，清理3个月前的车辆经纬度信息失败！" + e);
        }
    }

//    /**
//     * 每天晚上12点清空车辆缓存
//     */
//    @Scheduled(cron = "0 0 0 * * ?")
//    public void clearCarMap() {
//        try {
//            log.info("定时任务，清空车辆缓存开始" + new Date());
//            signCarService.clearCarMap();
//            log.info("定时任务，清空车辆缓存结束" + new Date());
//        } catch (Exception e) {
//            throw new RuntimeException("定时任务，清空车辆缓存失败！" + e);
//        }
//    }


//    /**
//     * 凌晨1点修改车辆状态
//     */
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void updateWorkOrderFileCreateTime() {
//        try {
//            log.info("定时任务，清空车辆缓存开始" + new Date());
//            signCarService.initCarStatusToNormal();
//            log.info("定时任务，清空车辆缓存结束" + new Date());
//        } catch (Exception e) {
//            throw new RuntimeException("定时任务，清空车辆缓存失败！" + e);
//        }
//    }



}
